package com.xoa.service.property;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.fixAssets.EduFixAssetsMapper;
import com.xoa.dao.property.AtabMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.fixAssets.EduFixAssets;
import com.xoa.model.property.Atab;
import com.xoa.model.property.Maintain;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AtabService {
    @Autowired
    AtabMapper atabMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    private UsersMapper usersMapper;

        public ToJson<Object> insertSelective(Atab record) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
          String de= record.getDepart ();
            String dep=de.substring(0,de.length()-1);
            record.setDepart ( dep );
            int count = atabMapper.insertSelective ( record );
            if (count > 0) {
                json.setMsg ( "创建成功" );
                json.setFlag ( 0 );
            }

        } catch (Exception e) {
            json.setMsg ( e.getMessage () );
            L.e ( "AtabService insertSelective:" + e );
        }
        return json;

    }


    public ToJson<Object> selectByPrimaryKey(Integer id, Atab record) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
            Atab atab = atabMapper.selectByPrimaryKey ( id );

            //领用部门
            String de= atab.getDepart ();
            String departName = departmentMapper.departmentOneSelect ( (Integer.parseInt ( de)) );

            //领用人
            String records = atab.getRecipient ();
            String[] split = records.split ( "," );
            String receive = "";
            if (split != null && split.length > 0) {
                for (String s : split) {
                    String name = usersMapper.getUsernameByUserId ( s );
                    if (name != null && name != "") {
                        receive = receive + name + ",";
                    }

                }
            }
            if (receive.length () > 0) {
                receive = receive.substring ( 0, receive.length () - 1 );
            }
            //是否归还处理
            String status = atab.getConditions ();
            int num = Integer.parseInt ( status );
            if (0 == num) {
                status = "是";
            }
            if (1 == num) {
                status = "否";
            }

            atab.setDepartment ( departName );//负责部门id
            atab.setUsers ( receive );//领用人id
            atab.setSta ( status );
            json.setObject ( atab );
            json.setMsg ( "ok" );
            json.setFlag ( 0 );
        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;

    }


    public ToJson<Object> deleteByPrimaryKey(Integer id) {
        ToJson<Object> toJson = new ToJson<> ();
        try {
            Integer delete = atabMapper.deleteByPrimaryKey ( id );
            if (delete > 0) {
                toJson.setMsg ( "删除成功" );
                toJson.setFlag ( 0 );
            }
        } catch (Exception e) {
            e.getMessage ();
            toJson.setMsg ( "删除失败" );
            toJson.setFlag ( 1 );
        }
        return toJson;
    }

    public ToJson<Object> updateByPrimaryKeySelective(Atab record) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
            int count = atabMapper.updateByPrimaryKeySelective ( record );
            if (count > 0) {
                json.setMsg ( "ok" );
                json.setFlag ( 0 );
            }

        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;
    }


    public ToJson<Object> selectByExampleWithBLOBs(String uid, Atab atab) {
      //  id ="GDZC2018071500004";
        ToJson<Object> json = new ToJson<Object> ();
        List<Object> result = new ArrayList<Object> ();
        Map<String, Object> map = new HashMap<String, Object> ();
        map.put ( "Atab", atab.getUid() );
        List<Atab> atabs = atabMapper.selectByExample(map);

        try {
            if (atabs != null) {//判断是否为空
                for (Atab a : atabs) {
                    Atab at = new Atab ();
                  if(uid.equals (a.getUid () )){//判断uid 和id是否相等
                        //领用部门
                        if (a.getDepart () == null) {
                            String departName = a.getDepart ();
                            departName = "";
                            at.setDepart ( departName );
                        } else {
                            String departName = departmentMapper.departmentOneSelect ( Integer.parseInt ( a.getDepart () ) );
                            at.setDepart ( departName );
                        }


                        //领用人
                        if (a.getRecipient () == null) {
                            String receive = a.getRecipient();
                            receive="";
                            at.setRecipient ( receive );//领用人
                        } else {
                            String records = a.getRecipient ();
                            String[] split = records.split ( "," );
                            String receive = "";
                            if (split != null && split.length > 0) {
                                for (String s : split) {
                                    String name = usersMapper.getUsernameByUserId ( s );
                                    if (name != null && name != "") {
                                        receive = receive + name + ",";
                                    }

                                }
                            }
                            if (receive.length () > 0) {
                                receive = receive.substring ( 0, receive.length () - 1 );
                            }
                            at.setRecipient ( receive );//领用人
                        }
                        //判断是否领用
                      String status=a.getConditions ();
                      int num=Integer.parseInt(status);
                      if(0==num){
                          status="是";
                      }
                      if(1==num){
                          status="否";
                      }

                        at.setId ( a.getId () );
                        at.setCreateTime ( a.getCreateTime () );//领用时间
                        at.setDepartment ( a.getDepart() );//领用部门id
                        at.setUsers ( a.getRecipient() );//领用人id
                        at.setConditions ( status );//是否归还
                        at.setEndtime ( a.getEndtime ());//归还时间
                        at.setRemarks( a.getRemarks() );//备注
                        at.setSta ( a.getConditions() );
                        result.add ( at );
                    }
                }
            }
            if (result != null) {
                json.setObj ( result );
                json.setMsg ( "ok" );
                json.setFlag ( 0 );
            }
        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;
    }
}
